﻿@using Topro.Entity.Enum;
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="easyui-layout case" data-options="fit: true">
    <div data-options="region: 'center', border: false" style="padding:10px">
        <!--header-->
        <div data-options="region:'north',border: false,split:true" style="height:40px;">
            <div class="data-tools clearfix">
                <div class="pull-left form-inline ">
                    <a class="btn btn-default btn-sm" data-modal="/Auth/MenuAddOrEdit" data-title="新增" data-width="600" data-height="500"><span class="glyphicon glyphicon-plus"></span> 新增</a>
                </div>
                <div class="pull-right">
                </div>
            </div>
        </div>
        <div data-options="region:'center',split: false, border: false">
            <!-- 表格数据 -->
            <table class="easyui-treegrid" id="menuTG"></table>
        </div>
    </div>
</div>
<script type="text/javascript">

    var menuTG = $('#menuTG');

    $(function () {
        //搜索
        $(".search").click(function(){
            page.search();
        });


        $(window).bind('load', function () {
            var winH = $(window).height() - 100;

            var top = $('.data-tools').offset().top;
            //工具栏高度
            var toolH = $('.data-tools').outerHeight();

            menuTG.css({
                height: (winH - toolH)
            });
        }).bind('resize', function () {//窗口改变
            var winH = $(window).height() - 100;
            //工具栏高度
            var toolH = $('.data-tools').outerHeight();

            menuTG.css({
                height: (winH - toolH)
            });
        });

        page.doSearch();

    });

    function Tip_success(word) {
        Topro.UI.Tip_success(word);
    }

    var page = {
        view: function (obj) {
            Topro.UI.DialogOpen($(obj).data("href") + '?v=' + Math.random().toString(36).substr(2, 4), {
                title: $(obj).data("title"),
                width: $(obj).data("width"),
                height: $(obj).data("height")
            });
        },
        search: function () {
            //表单
            var searchform = $("#searchForm").serializeArray();
            //表单转为Json
            var queryParams = page.getJson(searchform);

            menuTG.datagrid({
                queryParams: queryParams
            });

            return false;
        },
        getJson: function (array) {
            if (array != undefined && array.length > 0) {
                var json = {};
                $(array).each(function (i, v) {
                    json[v.name] = v.value;
                });
                return json;
            } else {
                return {};
            }
        },
        queryParams:function(){
            var params = {
            };

            return params;
        },
        doSearch: function () {

            //固定行
            var columns = [
                [
                    {
                        field: 'name',
                        title: '名称',
                        width: 200,
                        align: 'left',
                    },
                    {
                        field: 'displayOrder',
                        title: '排放',
                        width: 120,
                        align: 'center',
                        formatter: function (value, row, index) {
                            var result = "";

                            if (!Topro.Util.isNullOrEmptySpance(value)) {
                                result = value;
                            }
                            return result;
                        }
                    },
                    {
                        field: 'status',
                        title: '状态',
                        align: 'center',
                        width: 75,
                        formatter: function (value, row, index) {

                            var result = "";

                            if (value == @((sbyte)MenuStatus.启用)) {
                                result = "<span class='green bold'>启用</span>";
                            } else if (value == @((sbyte)MenuStatus.禁用)) {
                                result = "<span class='red bold'>禁用</span>";
                            }

                            return result;

                        }
                    },

                    {
                        field: 'createdTime',
                        title: '创建时间',
                        align: 'center',
                        width: 150,
                        formatter: function (value, row, index) {

                            var result = value.replace("T"," ");

                            return result;
                        }
                    },
                    {
                        field: 'id',
                        title: '操作',
                        width: 120,
                        align: 'center',
                        formatter: function (value, row, index) {

                            var result = "";

                            if (row.status == @((sbyte)MenuStatus.启用)) {
                                result += "<a class=\"btn btn-danger btn-xs\" data-confirm=\"确定要禁用该容器？\" data-succesFun=\"page.doSearch()\" data-href=\"/Auth/UpdateMenuStatus?id={0}&status={1}\" onclick=\"return Topro.UI.DialogConfirm(this)\">禁用</a>".format(row.id,@((sbyte)MenuStatus.禁用));
                            } else if (row.status == @((sbyte)MenuStatus.禁用)) {
                                result += "<a class=\"btn btn-primary btn-xs\" data-confirm=\"确定要启用该容器？\" data-succesFun=\"page.doSearch()\" data-href=\"/Auth/UpdateMenuStatus?id={0}&status={1}\" onclick=\"return Topro.UI.DialogConfirm(this)\">启用</a>".format(row.id,@((sbyte)MenuStatus.启用));
                                result += "<button type=\"button\" class=\"btn btn-default btn-xs m5_l\" data-href=\"/Auth/MenuAddOrEdit/{0}\" data-title=\"编辑\" data-width=\"600\" data-height=\"500\" onclick=\"return Topro.UI.View(this)\"><span class=\"glyphicon glyphicon-edit\"></span> 编辑</button>".format(row.id);
                            }

                            return result;
                        }
                    }
                ]
            ];

            var winH = $(window).height();
            //工具栏高度
            var toolH = $('.data-tools').outerHeight();
            //高度
            var height = winH - toolH - 20;

            menuTG.treegrid({
                url: '@Url.Action("MenuQuery", "Auth")',
                nowrap: false,
                rownumbers: true,
                singleSelect: false,
                fitColumns: false,
                idField: 'id',
                treeField: 'name',
                height: height,
                loadMsg: '正在加载数据……',
                animate: true,
                columns: columns
            });
        }
    }
</script>